const fs = require("fs");
const { authorverify } = require("../middleware/auth.middleware");

module.exports = function (app) {
  fs.readdirSync(__dirname).forEach((file) => {
    if (file == "index.js") return;
    app.use(require(`./${file}`).routes());
    app.use(require(`./${file}`).allowedMethods());
  });

  //websocket需要单独处理
  const dir = {};
  const router = require("koa-router")();
  router.all("/ws/changestatenotice", (ctx) => {
    const { myid } = ctx.request.query;
    dir[myid] = ctx;
    ctx.websocket.on("message", (data) => {
      dir[JSON.parse(data.toString()).toid]?.websocket.send("更新数据了");
    });
  });
  app.ws.use(router.routes()).use(router.allowedMethods());
};
